package com.tiyende.credit.loop.utils.helper

import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject
import java.util.Locale

object TiyendeLanguageHelper {

    /**
     * 获取语言列表
     */
    fun queryTiyendeLanguageList(): ArrayList<JSONObject>{
        val lList = arrayListOf<JSONObject>()
        //English
        val enItem = JSONObject()
        enItem.put("title",TiyendeApp.app.getString(R.string.tiyende_language_en))
        enItem.put("tags","en")
        lList.add(enItem)
        //Khmer
        val kmItem = JSONObject()
        kmItem.put("title",TiyendeApp.app.getString(R.string.tiyende_language_km))
        kmItem.put("tags","km")
        lList.add(kmItem)

        return lList
    }

    /**
     * 获取当前应用使用语言
     */
    fun queryTiyendeCurrentLanguage(): String{
        val locale = AppCompatDelegate.getApplicationLocales()[0]
        locale?.let {
            return locale.language
        }
        return ""
    }

    /**
     * 设置应用语言
     */
    fun saveTiyendeLanguage(language: String){
        //保存选择的语言
        TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_LANGUAGE_TAGS,language)
        AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(language))
    }
}